#!/usr/bin/env python
#-*- coding:utf-8 -*-

class PathPoint(object):
    def __init__(self, pid=None, pos=None):
        self.pid = pid
        self.pos = pos
    
    @property
    def pid(self):
        return self._pid

    @pid.setter
    def pid(self, pid):
        self._pid = pid
    
    @property
    def pos(self):
        return self._pos

    @pos.setter
    def pos(self, pos):
        self._pos = pos

    def __str__(self):
        _res = {'id': self.pid, 'pos': self.pos}
        return str(_res)

    def __repr__(self):
        _res = {'id': self.pid, 'pos': self.pos}
        return str(_res)


class Path(list): 
    def __add__(self, other):
        if isinstance(other, Path):
            tmp = self.copy()
            tmp.extend(other)
            return Path(tmp)
        return None

    @property    
    def start(self):
        if self:
            return self[0]
        else:
            return None

    @property
    def end(self):
        if self:
            return self[-1]
        else:
            return None

    @property
    def displayer(self):
        return self._displayer

    @displayer.setter
    def displayer(self, displayer):
        self._displayer = displayer

    def draw_path(self, trans=False):
        for i in range(len(self)-1):
            from_pos = self[i].pos
            to_pos = self[i + 1].pos
            x = [from_pos[0], to_pos[0]]
            y = [from_pos[1], to_pos[1]]
            if trans:
                x, y = y, x
            self.displayer.plot(x, y, color='magenta')

    def draw_endpoints(self, trans=False):
        sx, sy = self.start.pos[0], self.start.pos[1]
        ex, ey = self.end.pos[0], self.end.pos[1]
        if trans:
            sx, sy = sy, sx
            ex, ey = ey, ex
        self.displayer.plot(sx, sy, 'bo', color='green', markersize=14) #需要对x,y坐标进行转置
        self.displayer.plot(ex, ey, 'bo', color='red', markersize=14) #需要对x,y坐标进行转置

    def draw(self, displayer=None, trans=False):
        if displayer is not None:
            self.displayer = displayer
        self.draw_path(trans=trans)
        self.draw_endpoints(trans=trans)

    def savefig(self, fname=None, displayer=None):
        if displayer is not None:
            self.displayer = displayer
        self.displayer.savefig(fname=fname)

    def reverse(self):
        return Path(self[-1::-1])


if __name__ == "__main__":
    a = [1, 2, 4]
    p = Path(a)
    print(p)